<?php

namespace app\payment\admin;

use app\common\builder\ZBuilder;
use app\payment\model\Order as OrderModel;
use app\payment\model\Paid as PaidOrderModel;
use app\payment\model\AlipayPaid as AlipayPaidOrderModel;
use app\payment\model\PaymentPayCompany as PaymentPayCompanyModel;
use app\payment\model\PaymentPaidCompany as PaymentPaidCompanyModel;
use app\payment\model\PaymentAgentCompany as PaymentAgentCompanyModel;
use app\payment\model\Mconfig as MconfigModel;

/**
 * 报表分析控制器
 */
class Report extends Index
{

    protected function initialize()
    {
        parent::initialize();
        if (!in_array(request()->action(), ['index', 'agent_profit', 'agent_company', 'order_time', 'paid_order_time'])) {
            if ($this->no_auth) {
                $this->error('权限不足！');
            }
        }
    }

    /**
     * 商户报表分析
     * @return mixed
     */
    public function index()
    {
        set_time_limit(0);

        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));

        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        $auth = true;
        if ($this->no_auth) {
            $merchant_id = $this->merchant_id;
            $auth = false;
        }
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $all_type_array = OrderModel::where($map)
            ->distinct(true)
            ->column('type');

        sort($all_type_array);

        $now_data = [];
        $now_order_count_sum = 0;
        $now_order_count_ok = 0;
        $now_order_cash_sum = 0;
        $now_order_cash_ok = 0;
        // 交易统计(支付类型)
        foreach ($all_type_array as $type) {
            if (!$pay_type) {
                $map[] = ['type', '=', $type];
            }
            $now_data[$type]['now_order_count_sum'] = OrderModel::where($map)->count();
            $now_data[$type]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $now_data[$type]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $now_data[$type]['now_order_cash_sum'] /= 100;
            $now_data[$type]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $now_data[$type]['now_order_cash_ok'] /= 100;
            static $now_order_count_sum = 0;
            static $now_order_count_ok = 0;
            static $now_order_cash_sum = 0;
            static $now_order_cash_ok = 0;
            $now_order_count_sum = $now_order_count_sum + $now_data[$type]['now_order_count_sum'];
            $now_order_count_ok = $now_order_count_ok + $now_data[$type]['now_order_count_ok'];
            $now_order_cash_sum = $now_order_cash_sum + $now_data[$type]['now_order_cash_sum'];
            $now_order_cash_ok = $now_order_cash_ok + $now_data[$type]['now_order_cash_ok'];
            // 去掉数组支付类型
            array_pop($map);
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $sum_count = count($all_type_array);

        $this->assign('sum_count', $sum_count);
        $this->assign('now_order_count_sum', $now_order_count_sum);
        $this->assign('now_order_count_ok', $now_order_count_ok);
        $this->assign('now_order_cash_sum', $now_order_cash_sum);
        $this->assign('now_order_cash_ok', $now_order_cash_ok);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('child_company', $child_company);
        $this->assign('pay_type', $pay_type);
        $this->assign('now_data', $now_data);
        $this->assign('auth', $auth);
        return $this->fetch();
    }

    /**
     * 代理利润分析
     * @return mixed
     */
    public function agent_profit()
    {
        set_time_limit(0);

        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));

        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        if ($this->no_auth) {
            $this->assign('no_auth', true);
            if (!$this->is_agent) {
                $this->error('权限不足！');
            }
            $merchant_id = $this->merchant_id;
        }

        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $all_agent = MconfigModel::where(['agent_status' => 1])->column('payment_type_rate,sup_merchant', 'id');
        if ($merchant_id) {
            $all_agent = MconfigModel::where(['agent_status' => 1])->where(['id' => $merchant_id])->column('payment_type_rate,sup_merchant', 'id');
        }

        $merchant_now_data = [];
        $merchant_now_data_sum = 0;
        // 最多两级返利
        foreach ($all_agent as $agent_one_key => $agent_one_value) {

            $merchant_now_data[$agent_one_key]['profit'] = 0;

            // 一级代理费率
            $agent_one_value_rate = json_decode($agent_one_value['payment_type_rate'], true);

            $agent_two = MconfigModel::where(['sup_merchant' => $agent_one_key])->column('payment_type_rate,sup_merchant', 'id');
            if ($agent_two) {
                foreach ($agent_two as $agent_two_key => $agent_two_value) {
                    // 二级代理费率
                    $agent_two_value_rate = json_decode($agent_two_value['payment_type_rate'], true);

                    $agent_merchant = MconfigModel::where(['sup_merchant' => $agent_two_key])->column('payment_type_rate,sup_merchant', 'id');
                    if ($agent_merchant) {
                        foreach ($agent_merchant as $agent_merchant_key => $agent_merchant_value) {

                            //$merchant_id_rate = json_decode($agent_merchant_value['payment_type_rate'], true);

                            $all_type_arrays = OrderModel::where($map)
                                ->where(['merchant_id' => $agent_merchant_key])
                                ->distinct(true)
                                ->column('type');

                            foreach ($all_type_arrays as $types) {
                                $merchant_order_sum = OrderModel::where($map)->where(['merchant_id' => $agent_merchant_key])->where(['type' => $types])->where(['status' => 2])->sum('cash');
                                $merchant_order_sum /= 100;
                                /*
                                  if (isset($agent_two_value_rate[$types])) {
                                  $merchant_now_data[$agent_two_key]['profit'] += $merchant_order_sum * ($merchant_id_rate[$types] - $agent_two_value_rate[$types]);
                                  }
                                 */
                                if (isset($agent_one_value_rate[$types])) {
                                    $merchant_now_data[$agent_one_key]['profit'] += $merchant_order_sum * ($agent_two_value_rate[$types] - $agent_one_value_rate[$types]);
                                }
                            }
                        }
                    } else {
                        $all_type_arrays = OrderModel::where($map)
                            ->where(['merchant_id' => $agent_two_key])
                            ->distinct(true)
                            ->column('type');

                        foreach ($all_type_arrays as $types) {
                            $merchant_order_sum = OrderModel::where($map)->where(['merchant_id' => $agent_two_key])->where(['type' => $types])->where(['status' => 2])->sum('cash');
                            $merchant_order_sum /= 100;
                            if (isset($agent_one_value_rate[$types])) {
                                $merchant_now_data[$agent_one_key]['profit'] += $merchant_order_sum * ($agent_two_value_rate[$types] - $agent_one_value_rate[$types]);
                            }
                        }
                    }
                }
            }
        }

        foreach ($merchant_now_data as $key => $value) {
            $merchant_now_data_sum += $merchant_now_data[$key]['profit'];
        }

        $merchant_sum_count = count($merchant_now_data);

        $this->assign('all_agent', $all_agent);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('merchant_sum_count', $merchant_sum_count);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('pay_type', $pay_type);
        $this->assign('merchant_now_data', $merchant_now_data);
        $this->assign('merchant_now_data_sum', $merchant_now_data_sum);
        return $this->fetch();
    }

    /**
     * 代理通道分析
     * @return mixed
     */
    public function agent_company()
    {
        // 所有的支付公司子商户号支付类型费率
        $all_child_company_type_rate = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('payment_type_rate', 'name');

        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));

        $maps = [];
        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        if ($this->no_auth) {
            $this->assign('no_auth', true);
            if (!$this->is_agent) {
                $this->error('权限不足！');
            }
            $maps[] = ['merchant_id', '=', $this->merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $maps[] = ['company_name', '=', $child_company];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $now_data = [];
        $agent_companys = PaymentAgentCompanyModel::where([])->where($maps)->select();

        foreach ($agent_companys as $agent_company_key => $agent_company_value) {
            $merchant_ids = $agent_company_value['merchant_id'];
            $child_companys = $agent_company_value['company_name'];

            $now_data[$merchant_ids][$child_companys]['now_order_count_sum'] = OrderModel::where($map)->where(['child_company' => $child_companys])->count();
            $now_data[$merchant_ids][$child_companys]['now_order_count_ok'] = OrderModel::where($map)->where(['child_company' => $child_companys])->where(['status' => 2])->count();
            $now_data[$merchant_ids][$child_companys]['now_order_cash_sum'] = OrderModel::where($map)->where(['child_company' => $child_companys])->sum('cash');
            $now_data[$merchant_ids][$child_companys]['now_order_cash_sum'] /= 100;
            $now_data[$merchant_ids][$child_companys]['now_order_cash_ok'] = OrderModel::where($map)->where(['child_company' => $child_companys])->where(['status' => 2])->sum('cash');
            $now_data[$merchant_ids][$child_companys]['now_order_cash_ok'] /= 100;

            $now_data[$merchant_ids][$child_companys]['now_order_profit'] = 0;

            $all_type_arrays = OrderModel::where($map)
                ->where(['child_company' => $child_companys])
                ->distinct(true)
                ->column('type');

            // 循环哪些支付类型
            foreach ($all_type_arrays as $types) {

                $agent_type_rate_array = json_decode($agent_company_value['payment_type_rate'], true);
                // 代理公司对应类型费率
                $agent_type_rate = $agent_type_rate_array[$types];

                $merchant_now_order_profit_temp = OrderModel::where($map)->where(['child_company' => $child_companys])->where(['type' => $types])->where(['status' => 2])->sum('cash');
                $child_company_type_rate_array = json_decode($all_child_company_type_rate[$child_companys], true);
                // 支付公司对应类型费率
                $child_company_type_rate = $child_company_type_rate_array[$types];
                $now_data[$merchant_ids][$child_companys]['now_order_profit'] = $now_data[$merchant_ids][$child_companys]['now_order_profit'] + $merchant_now_order_profit_temp * ($agent_type_rate - $child_company_type_rate) / 100;
            }
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('child_company', $child_company);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('pay_type', $pay_type);
        $this->assign('now_data', $now_data);
        $this->assign('agent_companys', $agent_companys);
        return $this->fetch();
    }

    /**
     * 代理层级分析
     * @return mixed
     */
    public function agent_level()
    {
        $list = MconfigModel::where(['agent_status' => 1])->whereOr("sup_merchant !=''")->field('id,sup_merchant')->select();
        $agent = get_sub_merchant_tree($list);

        $this->assign('agent', $agent);
        return $this->fetch();
    }

    /**
     * 支付通道报表分析
     * @return mixed
     */
    public function company()
    {
        $this->redirect('company_analyse');

        // 所有的支付类型
        $all_payment_type = pay_config('payment_type');
        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::order(['name' => 'asc'])->column('title', 'name');
        $child_company_array = array_keys($all_child_company);

        $query_time = input('query_time', date('Y-m-d'));
        $query_type = input('query_type', 'day');
        $query_class = input('query_class', 'cash');
        $query_class_title = $query_class == 'cash' ? '金额' : '笔数';
        $query_status = input('query_status', 'total');
        $query_chart = input('chart', '1');
        $child_company = input('child_company') ? input('child_company') : $child_company_array[0];

        // 总支付公司名称
        $payment_company = strstr($child_company, '_', true);
        $payment_company_config = pay_config('', $payment_company);
        // 支付公司提供的支付类型
        $payment_types = $payment_company_config['supply_payment_type'];

        $map[] = ['child_company', '=', $child_company];
        $map[] = ['create_time', 'between time', ["{$query_time} 00:00:00", "{$query_time} 23:59:59"]];
        $now_data = [];
        // 交易统计
        foreach ($payment_types as $type) {
            $map[] = ['type', '=', $type];
            $now_data[$type]['now_order_count_sum'] = OrderModel::where($map)->count();
            $now_data[$type]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $now_data[$type]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $now_data[$type]['now_order_cash_sum'] /= 100;
            $now_data[$type]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $now_data[$type]['now_order_cash_ok'] /= 100;
            $type_name[] = $all_payment_type[$type];
            array_pop($map);
        }

        // 状态选择
        if ($query_status == 'complete') {
            $map[] = ['status', '=', 2];
        }

        if ($query_type == 'week') {
            $title = '一周交易金额统计 ' . "($query_class_title)";
            for ($index = 6; $index >= 0; $index--) {
                // 过去一周统计
                $date = date("Y-m-d", (strtotime($query_time) - 3600 * 24 * $index));
                $all_date[] = $date;
                foreach ($payment_types as $type) {
                    $map[] = ['type', '=', $type];
                    $map[] = ['create_time', 'between time', ["$date 00:00:00", "$date 23:59:59"]];

                    if ($query_class == 'count') {
                        $show_data[$type][$date]['count'] = OrderModel::where($map)->count();
                    }
                    if ($query_class == 'cash') {
                        $show_data[$type][$date]['cash'] = OrderModel::where($map)->sum('cash');
                        $show_data[$type][$date]['cash'] /= 100;
                    }
                    array_pop($map);
                    array_pop($map);
                }
            }
        }

        if ($query_type == 'day') {
            $title = '一天交易金额统计 ' . "($query_class_title)";
            if ($query_time == date("Y-m-d")) {
                $hour = intval(date('H'));
            } else {
                $hour = 24;
            }
            for ($index = 0; $index <= $hour; $index++) {
                // 小时统计
                $date = $index;
                $all_date[] = $date;
                foreach ($payment_types as $type) {
                    $map[] = ['type', '=', $type];
                    $map[] = ['create_time', 'between time', ["$query_time $date:00:00", "$query_time $date:59:59"]];

                    if ($query_class == 'count') {
                        $show_data[$type][$date]['count'] = OrderModel::where($map)->count();
                    }
                    if ($query_class == 'cash') {
                        $show_data[$type][$date]['cash'] = OrderModel::where($map)->sum('cash');
                        $show_data[$type][$date]['cash'] /= 100;
                    }
                    array_pop($map);
                    array_pop($map);
                }
            }
        }

        // 图表数组准备转换json
        foreach ($show_data as $type => $value) {
            static $i = 0;
            $series[$i]['name'] = $all_payment_type[$type];
            $series[$i]['type'] = 'line';
            $series[$i]['stack'] = '总量';
            $series[$i]['label']['normal']['show'] = 'true';
            $series[$i]['label']['normal']['position'] = 'top';
            $series[$i]['areaStyle'] = '{normal: {}}';
            foreach ($all_date as $one_date) {
                $series[$i]['data'][] = $value[$one_date][$query_class];

                if (isset($sum_data[$one_date])) {
                    $sum_data[$one_date] = $sum_data[$one_date] + $value[$one_date][$query_class];
                } else {
                    $sum_data[$one_date] = $value[$one_date][$query_class];
                }
            }
            $i++;
        }
        $sum_data = array_values($sum_data);

        $series[] = [
            'name' => '支付',
            'type' => 'line',
            'stack' => '总量',
            'label' => [
                'normal' => [
                    'show' => 'true',
                    'position' => 'top',
                ],
            ],
            'areaStyle' => '{normal: {}}',
            'data' => $sum_data,
        ];

        foreach ($series as $key => &$value) {

            if ($query_chart == 1) {
                // 默认图一
            }
            if ($query_chart == 2) {
                // 图二
                unset($value['label']);
            }
            if ($query_chart == 3) {
                // 图三
                unset($value['areaStyle']);
            }
            if ($query_chart == 4) {
                // 图四
                unset($value['label']);
                unset($value['areaStyle']);
            }
        }

        $type_name[] = '支付';
        $type_name = json_encode($type_name, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        $all_date = json_encode($all_date, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        $series = json_encode($series, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

        $REQUEST_URI = str_replace("amp;", "", request()->server('REQUEST_URI'));
        $pos = strrpos($REQUEST_URI, "?");
        if ($pos === false) {
            $url = $REQUEST_URI . '?';
        } else {
            $url = $REQUEST_URI . '&';
        }

        $this->assign('url', $url);
        $this->assign('title', $title);
        $this->assign('query_time', $query_time);
        $this->assign('query_type', $query_type);
        $this->assign('query_class', $query_class);
        $this->assign('query_status', $query_status);
        $this->assign('all_child_company', $all_child_company);
        $this->assign('child_company', $child_company);
        $this->assign('type_name', $type_name);
        $this->assign('series', $series);
        $this->assign('now_data', $now_data);
        $this->assign('all_payment_type', $all_payment_type);
        $this->assign('show_data', $show_data);
        $this->assign('all_date', $all_date);

        return $this->fetch();
    }

    /**
     * 公司对商户分析
     * @return mixed
     */
    public function ctom()
    {
        // 所有的支付公司
        $all_config_group = pay_config('config_group');
        unset($all_config_group['pay_config']);

        $query_type = input('query_type', 'open');
        $query_company = input('query_company', 'child');
        $child_company = input('child_company');

        $company_where = $query_type == 'open' ? ['status' => 1] : [];
        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where($company_where)->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $all_company = $query_company == 'child' ? $all_child_company : $all_config_group;
        $company = $query_company == 'child' ? 'child_company' : 'company';

        $all_merchant = MconfigModel::order('create_time desc')->select();

        $now_data = [];

        if ($child_company) {
            if (isset($all_company[$child_company])) {
                $all_companys[$child_company] = $all_company[$child_company];
            } else {
                $all_companys = $all_company;
            }
        } else {
            $all_companys = $all_company;
        }

        // 公司循环
        foreach ($all_companys as $c_key => $c_value) {
            // 商户号循环
            foreach ($all_merchant as $m_key => $m_value) {
                if (strpos($m_value['type_company_config'], $c_key) !== false) {
                    $now_data[$c_key][] = $m_value['id'];
                }
            }
        }

        $this->assign('query_type', $query_type);
        $this->assign('query_company', $query_company);
        $this->assign('all_company', $all_company);
        $this->assign('child_company', $child_company);
        $this->assign('now_data', $now_data);

        return $this->fetch();
    }

    /**
     * 商户对公司分析
     * @return mixed
     */
    public function mtoc()
    {
        // 设置搜索参数
        $search = ['id' => '商户号', 'payment_type' => '支付类型', 'type_company_config' => '支付类型和支付公司配置'];
        // 获取查询条件
        $map = $this->getMap();
        if ($this->no_auth) {
            $map[] = ['id', '=', $this->merchant_id];
            $search = [];
        }
        // 数据列表
        $data_list = MconfigModel::where($map)->order('create_time desc')->paginate();

        // 分页数据
        $page = $data_list->render();

        // 所有的支付类型
        $all_payment_type = pay_config('payment_type');
        // 显示所有的支付公司下的商户号的名称和标题
        $all_pay_company = PaymentPayCompanyModel::column('title', 'name');
        // 显示所有的代付公司下的商户号的名称和标题
        $all_paid_company = PaymentPaidCompanyModel::column('title', 'name');

        $items = [
            ['id', '商户', 'callback', function ($value) {
                return $value . '<br/>' . get_user_nickname($value);
            }, '', 'text-center'],
            ['payment_type', '支付类型', 'callback', function ($payment_type, $all_payment_type) {

                $payment_type = json_decode($payment_type, true);
                foreach ($payment_type as $value) {
                    $arr[] = $all_payment_type[$value];
                }
                $payment_type_string = implode('<br/>', $arr);

                return $payment_type_string; // 可以用$data接收到其他字段的数据
            }, $all_payment_type],
            ['type_company_config', '支付类型对应子公司', 'callback', function ($type_company_config, $all_pay_company) {

                // 所有的支付类型
                $all_payment_type = pay_config('payment_type');

                $type_company_config_array = json_decode($type_company_config, true);

                $temp = '';
                foreach ($type_company_config_array as $type => $companys) {
                    $type = isset($all_payment_type[$type]) ? $all_payment_type[$type] : $type;
                    $temp = $temp . $type . '—》<br/>';
                    foreach ($companys as $value) {

                        $value = isset($all_pay_company[$value]) ? $all_pay_company[$value] : $value;
                        $temp = $temp . $value . '<br/>';
                    }
                    $temp = $temp . '<br/>';
                }

                return $temp;
            }, $all_pay_company],
            ['paid_company', '代付公司', 'callback', function ($value, $all_paid_company) {

                $temp = '';
                if ($value['paid_company_config']) {
                    $paid_company = json_decode($value['paid_company_config'], true);
                    $temp = $temp . '银行卡代付公司' . '—》<br/>';
                    foreach ($paid_company as $p_c) {

                        $p_c = isset($all_paid_company[$p_c]) ? $all_paid_company[$p_c] : $p_c;
                        $temp = $temp . $p_c . '<br/>';
                    }
                }

                $temp = $temp . '<br/>';

                if ($value['alipay_paid_company_config']) {
                    $paid_company = json_decode($value['alipay_paid_company_config'], true);
                    $temp = $temp . '支付宝代付公司' . '—》<br/>';
                    foreach ($paid_company as $p_c) {

                        $p_c = isset($all_paid_company[$p_c]) ? $all_paid_company[$p_c] : $p_c;
                        $temp = $temp . $p_c . '<br/>';
                    }
                }
                return $temp;
            }, '__data__', '', $all_paid_company],
        ];

        $map[] = ['manage_merchant', '<>', ''];
        $find = MconfigModel::where($map)->find();
        if ($find) {
            $items[] = ['manage_merchant', '管理商户号', 'callback', function ($value) {
                $temp = '';
                if ($value) {
                    $manage_merchant = json_decode($value, true);
                    foreach ($manage_merchant as $v) {
                        $temp = $temp . $v . '—》' . get_user_nickname($v) . '<br/>';
                    }
                }
                return $temp;
            }, '', 'text-center'];
        }

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('商户对公司') // 设置页面标题
            ->setTableName('payment_merchant_config') // 设置数据表名
            ->setPrimaryKey('id') // 设置主键名为id
            ->setSearch($search) // 设置搜索参数
            ->addColumns($items)
            ->hideCheckbox()
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    /**
     * 通道订单分析
     * @return mixed
     */
    public function company_analyse()
    {
        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));
        $time = input('time', 0);
        // 查看最近时间,重置初始化起始时间
        if ($time) {
            $now_time = strtotime('now') - $time;
            $query_begin_time = date('Y-m-d H:i:s', $now_time);
        }
        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $all_child_company_array = OrderModel::where($map)
            ->distinct(true)
            ->column('child_company');

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        // 排序
        $all_child_company_arrays = [];
        foreach ($all_child_company as $all_child_company_k => $all_child_company_v) {
            if (in_array($all_child_company_k, $all_child_company_array)) {
                $all_child_company_arrays[] = $all_child_company_k;
            }
        }

        $now_data = [];
        $now_order_count_sum = 0;
        $now_order_count_ok = 0;
        $now_order_cash_sum = 0;
        $now_order_cash_ok = 0;
        // 交易统计
        foreach ($all_child_company_arrays as $child_company) {
            $map[] = ['child_company', '=', $child_company];
            $now_data[$child_company]['now_order_count_sum'] = OrderModel::where($map)->count();
            $now_data[$child_company]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $now_data[$child_company]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $now_data[$child_company]['now_order_cash_sum'] /= 100;
            $now_data[$child_company]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $now_data[$child_company]['now_order_cash_ok'] /= 100;
            static $now_order_count_sum = 0;
            static $now_order_count_ok = 0;
            static $now_order_cash_sum = 0;
            static $now_order_cash_ok = 0;
            $now_order_count_sum = $now_order_count_sum + $now_data[$child_company]['now_order_count_sum'];
            $now_order_count_ok = $now_order_count_ok + $now_data[$child_company]['now_order_count_ok'];
            $now_order_cash_sum = $now_order_cash_sum + $now_data[$child_company]['now_order_cash_sum'];
            $now_order_cash_ok = $now_order_cash_ok + $now_data[$child_company]['now_order_cash_ok'];
            array_pop($map);
        }

        $sum_count = count($all_child_company_array);

        $this->assign('sum_count', $sum_count);
        $this->assign('now_order_count_sum', $now_order_count_sum);
        $this->assign('now_order_count_ok', $now_order_count_ok);
        $this->assign('now_order_cash_sum', $now_order_cash_sum);
        $this->assign('now_order_cash_ok', $now_order_cash_ok);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('time', $time);
        $this->assign('pay_type', $pay_type);
        $this->assign('now_data', $now_data);
        return $this->fetch();
    }

    /**
     * 商户订单分析
     * @return mixed
     */
    public function merchant_analyse()
    {
        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));
        $time = input('time', 0);
        // 查看最近时间,重置初始化起始时间
        if ($time) {
            $now_time = strtotime('now') - $time;
            $query_begin_time = date('Y-m-d H:i:s', $now_time);
        }
        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $all_merchant_id_array = OrderModel::where($map)
            ->distinct(true)
            ->column('merchant_id');

        sort($all_merchant_id_array);

        $now_data = [];
        $now_order_count_sum = 0;
        $now_order_count_ok = 0;
        $now_order_cash_sum = 0;
        $now_order_cash_ok = 0;
        // 交易统计
        foreach ($all_merchant_id_array as $merchant_id) {
            $map[] = ['merchant_id', '=', $merchant_id];
            $now_data[$merchant_id]['now_order_count_sum'] = OrderModel::where($map)->count();
            $now_data[$merchant_id]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $now_data[$merchant_id]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $now_data[$merchant_id]['now_order_cash_sum'] /= 100;
            $now_data[$merchant_id]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $now_data[$merchant_id]['now_order_cash_ok'] /= 100;
            static $now_order_count_sum = 0;
            static $now_order_count_ok = 0;
            static $now_order_cash_sum = 0;
            static $now_order_cash_ok = 0;
            $now_order_count_sum = $now_order_count_sum + $now_data[$merchant_id]['now_order_count_sum'];
            $now_order_count_ok = $now_order_count_ok + $now_data[$merchant_id]['now_order_count_ok'];
            $now_order_cash_sum = $now_order_cash_sum + $now_data[$merchant_id]['now_order_cash_sum'];
            $now_order_cash_ok = $now_order_cash_ok + $now_data[$merchant_id]['now_order_cash_ok'];
            array_pop($map);
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $sum_count = count($all_merchant_id_array);

        $this->assign('sum_count', $sum_count);
        $this->assign('now_order_count_sum', $now_order_count_sum);
        $this->assign('now_order_count_ok', $now_order_count_ok);
        $this->assign('now_order_cash_sum', $now_order_cash_sum);
        $this->assign('now_order_cash_ok', $now_order_cash_ok);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('child_company', $child_company);
        $this->assign('time', $time);
        $this->assign('pay_type', $pay_type);
        $this->assign('now_data', $now_data);
        return $this->fetch();
    }

    /**
     * 商户明细分析
     * @return mixed
     */
    public function merchant_detailed()
    {
        set_time_limit(0);
        // 所有的支付公司子商户号支付类型费率
        $all_child_company_type_rate = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('payment_type_rate', 'name');

        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));

        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $all_type_array = OrderModel::where($map)
            ->distinct(true)
            ->column('type');

        sort($all_type_array);

        $now_data = [];
        $now_order_count_sum = 0;
        $now_order_count_ok = 0;
        $now_order_cash_sum = 0;
        $now_order_cash_ok = 0;
        // 交易统计(支付类型)
        foreach ($all_type_array as $type) {
            if (!$pay_type) {
                $map[] = ['type', '=', $type];
            }
            $now_data[$type]['now_order_count_sum'] = OrderModel::where($map)->count();
            $now_data[$type]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $now_data[$type]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $now_data[$type]['now_order_cash_sum'] /= 100;
            $now_data[$type]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $now_data[$type]['now_order_cash_ok'] /= 100;
            static $now_order_count_sum = 0;
            static $now_order_count_ok = 0;
            static $now_order_cash_sum = 0;
            static $now_order_cash_ok = 0;
            $now_order_count_sum = $now_order_count_sum + $now_data[$type]['now_order_count_sum'];
            $now_order_count_ok = $now_order_count_ok + $now_data[$type]['now_order_count_ok'];
            $now_order_cash_sum = $now_order_cash_sum + $now_data[$type]['now_order_cash_sum'];
            $now_order_cash_ok = $now_order_cash_ok + $now_data[$type]['now_order_cash_ok'];
            // 去掉数组支付类型
            array_pop($map);
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        // 商户统计信息
        $all_merchant_id_array = OrderModel::where($map)
            ->distinct(true)
            ->column('merchant_id');

        sort($all_merchant_id_array);

        $merchant_now_data = [];
        $merchant_now_order_count_sum = 0;
        $merchant_now_order_count_ok = 0;
        $merchant_now_order_cash_sum = 0;
        $merchant_now_order_cash_ok = 0;
        $merchant_now_order_service_fee = 0;
        $merchant_now_order_balance = 0;
        $merchant_now_order_profit = 0;
        // 交易统计
        foreach ($all_merchant_id_array as $merchant_ids) {
            if (!$merchant_id) {
                $map[] = ['merchant_id', '=', $merchant_ids];
            }
            $merchant_now_data[$merchant_ids]['now_order_count_sum'] = OrderModel::where($map)->count();
            $merchant_now_data[$merchant_ids]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $merchant_now_data[$merchant_ids]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $merchant_now_data[$merchant_ids]['now_order_cash_sum'] /= 100;
            $merchant_now_data[$merchant_ids]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $merchant_now_data[$merchant_ids]['now_order_cash_ok'] /= 100;

            static $merchant_now_order_count_sum = 0;
            static $merchant_now_order_count_ok = 0;
            static $merchant_now_order_cash_sum = 0;
            static $merchant_now_order_cash_ok = 0;
            $merchant_now_order_count_sum = $merchant_now_order_count_sum + $merchant_now_data[$merchant_ids]['now_order_count_sum'];
            $merchant_now_order_count_ok = $merchant_now_order_count_ok + $merchant_now_data[$merchant_ids]['now_order_count_ok'];
            $merchant_now_order_cash_sum = $merchant_now_order_cash_sum + $merchant_now_data[$merchant_ids]['now_order_cash_sum'];
            $merchant_now_order_cash_ok = $merchant_now_order_cash_ok + $merchant_now_data[$merchant_ids]['now_order_cash_ok'];

            // 商户配置
            $merchant_config = MconfigModel::where(['id' => $merchant_ids])->find();
            $merchant_type_rate_array = json_decode($merchant_config['payment_type_rate'], true);

            $all_type_arrays = OrderModel::where($map)
                ->distinct(true)
                ->column('type');

            $all_child_company_array = OrderModel::where($map)
                ->distinct(true)
                ->column('child_company');

            $merchant_now_order_profit_temp = 0;

            // 初始化商户收单服务费
            $merchant_now_data[$merchant_ids]['now_order_service_fee'] = 0;
            // 可结算金额
            $merchant_now_data[$merchant_ids]['now_order_balance'] = 0;
            // 初始化商户利润
            $merchant_now_data[$merchant_ids]['now_order_profit'] = 0;

            // 循环哪些支付类型
            foreach ($all_type_arrays as $types) {
                if (!$pay_type) {
                    $map[] = ['type', '=', $types];
                }
                static $merchant_now_order_service_fee = 0;
                static $merchant_now_order_balance = 0;

                // 商户号对应类型费率
                $merchant_type_rate = $merchant_type_rate_array[$types];

                // 完成金额
                $merchant_now_order_cash_ok_type = OrderModel::where($map)->where(['status' => 2])->sum('cash');
                $merchant_now_order_cash_ok_type /= 100;

                $merchant_now_data[$merchant_ids]['now_order_service_fee'] = $merchant_now_data[$merchant_ids]['now_order_service_fee'] + $merchant_now_order_cash_ok_type * $merchant_type_rate;
                $merchant_now_data[$merchant_ids]['now_order_balance'] = $merchant_now_data[$merchant_ids]['now_order_balance'] + $merchant_now_order_cash_ok_type * (1 - $merchant_type_rate);

                $merchant_now_order_service_fee = $merchant_now_order_service_fee + $merchant_now_order_cash_ok_type * $merchant_type_rate;
                $merchant_now_order_balance = $merchant_now_order_balance + $merchant_now_order_cash_ok_type * (1 - $merchant_type_rate);

                foreach ($all_child_company_array as $child_companys) {
                    if (!$child_company) {
                        $map[] = ['child_company', '=', $child_companys];
                    }
                    static $merchant_now_order_profit = 0;

                    $merchant_now_order_profit_temp = OrderModel::where($map)->where(['status' => 2])->sum('cash');
                    $child_company_type_rate_array = json_decode($all_child_company_type_rate[$child_companys], true);
                    $child_company_type_rate = $child_company_type_rate_array[$types];
                    $merchant_now_data[$merchant_ids]['now_order_profit'] = $merchant_now_data[$merchant_ids]['now_order_profit'] + $merchant_now_order_profit_temp * ($merchant_type_rate - $child_company_type_rate) / 100;

                    $merchant_now_order_profit = $merchant_now_order_profit + $merchant_now_order_profit_temp * ($merchant_type_rate - $child_company_type_rate) / 100;
                    // 去掉数组公司
                    if (!$child_company) {
                        array_pop($map);
                    }
                }
                // 去掉数组支付类型
                if (!$pay_type) {
                    array_pop($map);
                }
            }
            // 去掉数组商户号
            if (!$merchant_id) {
                array_pop($map);
            }
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $sum_count = count($all_type_array);
        $merchant_sum_count = count($all_merchant_id_array);

        $this->assign('sum_count', $sum_count);
        $this->assign('merchant_sum_count', $merchant_sum_count);
        $this->assign('now_order_count_sum', $now_order_count_sum);
        $this->assign('now_order_count_ok', $now_order_count_ok);
        $this->assign('now_order_cash_sum', $now_order_cash_sum);
        $this->assign('now_order_cash_ok', $now_order_cash_ok);
        $this->assign('merchant_now_order_count_sum', $merchant_now_order_count_sum);
        $this->assign('merchant_now_order_count_ok', $merchant_now_order_count_ok);
        $this->assign('merchant_now_order_cash_sum', $merchant_now_order_cash_sum);
        $this->assign('merchant_now_order_cash_ok', $merchant_now_order_cash_ok);
        $this->assign('merchant_now_order_service_fee', $merchant_now_order_service_fee);
        $this->assign('merchant_now_order_balance', $merchant_now_order_balance);
        $this->assign('merchant_now_order_profit', $merchant_now_order_profit);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('child_company', $child_company);
        $this->assign('pay_type', $pay_type);
        $this->assign('now_data', $now_data);
        $this->assign('merchant_now_data', $merchant_now_data);
        return $this->fetch();
    }

    /**
     * 通道商户利润
     * @return mixed
     */
    public function company_merchant_profit()
    {
        set_time_limit(0);
        // 所有的支付公司子商户号支付类型费率
        $all_child_company_type_rate = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('payment_type_rate', 'name');

        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));

        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        // 商户统计信息
        $all_merchant_id_array = OrderModel::where($map)
            ->distinct(true)
            ->column('merchant_id');

        sort($all_merchant_id_array);

        $all_child_company_array = OrderModel::where($map)
            ->distinct(true)
            ->column('child_company');

        sort($all_child_company_array);

        $merchant_now_data = [];
        $merchant_now_data_sum = [];
        $merchant_now_order_profit = 0;
        // 交易统计
        foreach ($all_merchant_id_array as $merchant_ids) {
            if (!$merchant_id) {
                $map[] = ['merchant_id', '=', $merchant_ids];
            }
            // 商户配置
            $merchant_config = MconfigModel::where(['id' => $merchant_ids])->find();
            $merchant_type_rate_array = json_decode($merchant_config['payment_type_rate'], true);

            $merchant_now_order_profit_temp = 0;
            $now_order_profit_merchant[$merchant_ids] = 0;

            foreach ($all_child_company_array as $child_companys) {

                $now_order_profit_merchant[$child_companys] = 0;

                $merchant_now_data[$merchant_ids][$child_companys]['now_order_profit'] = 0;

                $all_type_arrays = OrderModel::where($map)
                    ->where(['child_company' => $child_companys])
                    ->distinct(true)
                    ->column('type');

                // 循环哪些支付类型
                foreach ($all_type_arrays as $types) {

                    // 商户号对应类型费率
                    $merchant_type_rate = $merchant_type_rate_array[$types];

                    $merchant_now_order_profit_temp = OrderModel::where($map)->where(['child_company' => $child_companys])->where(['type' => $types])->where(['status' => 2])->sum('cash');
                    $child_company_type_rate_array = json_decode($all_child_company_type_rate[$child_companys], true);
                    // 支付公司对应类型费率
                    $child_company_type_rate = $child_company_type_rate_array[$types];
                    $merchant_now_data[$merchant_ids][$child_companys]['now_order_profit'] = $merchant_now_data[$merchant_ids][$child_companys]['now_order_profit'] + $merchant_now_order_profit_temp * ($merchant_type_rate - $child_company_type_rate) / 100;
                }
            }
            // 去掉数组商户号
            if (!$merchant_id) {
                array_pop($map);
            }
        }

        $all_sum = 0;
        foreach ($merchant_now_data as $merchant_keys => $child_company_values) {
            $merchant_now_data_sum[$merchant_keys] = 0;
            foreach ($child_company_values as $child_company_values_key => $child_company_values_value) {

                $merchant_now_data_sum[$merchant_keys] += $child_company_values[$child_company_values_key]['now_order_profit'];

                if (!isset($merchant_now_data_sum[$child_company_values_key])) {
                    $merchant_now_data_sum[$child_company_values_key] = 0;
                }

                $merchant_now_data_sum[$child_company_values_key] += $child_company_values[$child_company_values_key]['now_order_profit'];
            }
            $all_sum += $merchant_now_data_sum[$merchant_keys];
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $merchant_sum_count = count($all_merchant_id_array);
        $child_company_sum_count = count($all_child_company_array);

        $this->assign('merchant_sum_count', $merchant_sum_count);
        $this->assign('child_company_sum_count', $child_company_sum_count);
        $this->assign('all_merchant_id_array', $all_merchant_id_array);
        $this->assign('all_child_company_array', $all_child_company_array);
        $this->assign('merchant_now_order_profit', $merchant_now_order_profit);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('child_company', $child_company);
        $this->assign('pay_type', $pay_type);
        $this->assign('merchant_now_data', $merchant_now_data);
        $this->assign('merchant_now_data_sum', $merchant_now_data_sum);
        $this->assign('all_sum', $all_sum);
        return $this->fetch();
    }

    /**
     * 代付明细分析
     * @return mixed
     */
    public function paid()
    {
        set_time_limit(0);
        // 所有的代付公司配置
        $all_paid_company_company = PaymentPaidCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('*', 'name');

        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d') . ' 00:00:00'));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d') . ' 23:59:59'));

        $map[] = ['create_time', 'between time', [$query_begin_time, $query_end_time]];

        $merchant_id = input('merchant_id');
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $status = input('status', 2);
        if ($status) {
            $map[] = ['status', '=', $status];
        }

        $paid_mode = input('paid_mode');
        if ($paid_mode) {
            $map[] = ['mode', '=', $paid_mode - 1];
        }

        $paid_class = input('paid_class');

        $merchant_now_data = [];
        $merchant_now_data_sum = [];
        $all_child_company_array_bank = [];
        $all_child_company_array_alipay = [];
        $all_merchant_id_array_bank = [];
        $all_merchant_id_array_alipay = [];

        // 银行卡代付
        if ($paid_class != 2) {

            // 商户统计信息
            $all_merchant_id_array_bank = PaidOrderModel::where($map)
                ->distinct(true)
                ->column('merchant_id');

            sort($all_merchant_id_array_bank);

            $all_child_company_array_bank = PaidOrderModel::where($map)
                ->distinct(true)
                ->column('child_company');

            sort($all_child_company_array_bank);

            foreach ($all_merchant_id_array_bank as $merchant_ids) {

                foreach ($all_child_company_array_bank as $child_companys) {

                    $company_paid_start_price = $all_paid_company_company[$child_companys]['paid_start_price'];
                    $company_paid_rate = $all_paid_company_company[$child_companys]['paid_rate'];

                    $merchant_now_data[$merchant_ids][$child_companys]['count'] = PaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->count();
                    $merchant_now_data[$merchant_ids][$child_companys]['cash'] = PaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->sum('cash');
                    $merchant_now_data[$merchant_ids][$child_companys]['cash'] /= 100;
                    $merchant_now_data[$merchant_ids][$child_companys]['deduct_cash'] = PaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->sum('deduct_cash');
                    $merchant_now_data[$merchant_ids][$child_companys]['deduct_cash'] /= 100;

                    // 代付成功笔数
                    $merchant_company_count_success = PaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->where(['status' => 2])->count();
                    // 代付成功金额
                    $merchant_company_sum_cash_success = PaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->where(['status' => 2])->sum('cash');
                    $merchant_company_sum_cash_success /= 100;
                    // 代付成功金额扣除
                    $merchant_company_sum_deduct_cash_success = PaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->where(['status' => 2])->sum('deduct_cash');
                    $merchant_company_sum_deduct_cash_success /= 100;
                    // 代付成本
                    $merchant_company_cb = $merchant_company_count_success * $company_paid_start_price + $merchant_company_sum_cash_success + $merchant_company_sum_cash_success * $company_paid_rate;
                    // 利润
                    $merchant_now_data[$merchant_ids][$child_companys]['profit'] = $merchant_company_sum_deduct_cash_success - $merchant_company_cb;
                }
            }
        }
        // 支付宝代付
        if ($paid_class != 1) {

            // 商户统计信息
            $all_merchant_id_array_alipay = AlipayPaidOrderModel::where($map)
                ->distinct(true)
                ->column('merchant_id');

            sort($all_merchant_id_array_alipay);

            $all_child_company_array_alipay = AlipayPaidOrderModel::where($map)
                ->distinct(true)
                ->column('child_company');

            sort($all_child_company_array_alipay);

            foreach ($all_merchant_id_array_alipay as $merchant_ids) {

                foreach ($all_child_company_array_alipay as $child_companys) {

                    $company_paid_start_price = $all_paid_company_company[$child_companys]['paid_start_price'];
                    $company_paid_rate = $all_paid_company_company[$child_companys]['paid_rate'];

                    $merchant_now_data[$merchant_ids][$child_companys]['count'] = AlipayPaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->count();
                    $merchant_now_data[$merchant_ids][$child_companys]['cash'] = AlipayPaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->sum('cash');
                    $merchant_now_data[$merchant_ids][$child_companys]['cash'] /= 100;
                    $merchant_now_data[$merchant_ids][$child_companys]['deduct_cash'] = AlipayPaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->sum('deduct_cash');
                    $merchant_now_data[$merchant_ids][$child_companys]['deduct_cash'] /= 100;

                    // 代付成功笔数
                    $merchant_company_count_success = AlipayPaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->where(['status' => 2])->count();
                    // 代付成功金额
                    $merchant_company_sum_cash_success = AlipayPaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->where(['status' => 2])->sum('cash');
                    $merchant_company_sum_cash_success /= 100;
                    // 代付成功金额扣除
                    $merchant_company_sum_deduct_cash_success = AlipayPaidOrderModel::where($map)->where(['merchant_id' => $merchant_ids])->where(['child_company' => $child_companys])->where(['status' => 2])->sum('deduct_cash');
                    $merchant_company_sum_deduct_cash_success /= 100;
                    // 代付成本
                    $merchant_company_cb = $merchant_company_count_success * $company_paid_start_price + $merchant_company_sum_cash_success + $merchant_company_sum_cash_success * $company_paid_rate;
                    // 利润
                    $merchant_now_data[$merchant_ids][$child_companys]['profit'] = $merchant_company_sum_deduct_cash_success - $merchant_company_cb;
                }
            }
        }

        $all_sum['count'] = 0;
        $all_sum['cash'] = 0;
        $all_sum['deduct_cash'] = 0;
        $all_sum['profit'] = 0;

        foreach ($merchant_now_data as $merchant_keys => $child_company_values) {

            foreach ($child_company_values as $child_company_values_key => $child_company_values_value) {

                if (!isset($merchant_now_data_sum[$merchant_keys]['count'])) {
                    $merchant_now_data_sum[$merchant_keys]['count'] = 0;
                }
                if (!isset($merchant_now_data_sum[$merchant_keys]['cash'])) {
                    $merchant_now_data_sum[$merchant_keys]['cash'] = 0;
                }
                if (!isset($merchant_now_data_sum[$merchant_keys]['deduct_cash'])) {
                    $merchant_now_data_sum[$merchant_keys]['deduct_cash'] = 0;
                }
                if (!isset($merchant_now_data_sum[$merchant_keys]['profit'])) {
                    $merchant_now_data_sum[$merchant_keys]['profit'] = 0;
                }

                $merchant_now_data_sum[$merchant_keys]['count'] += $child_company_values[$child_company_values_key]['count'];
                $merchant_now_data_sum[$merchant_keys]['cash'] += $child_company_values[$child_company_values_key]['cash'];
                $merchant_now_data_sum[$merchant_keys]['deduct_cash'] += $child_company_values[$child_company_values_key]['deduct_cash'];
                $merchant_now_data_sum[$merchant_keys]['profit'] += $child_company_values[$child_company_values_key]['profit'];

                if (!isset($merchant_now_data_sum[$child_company_values_key]['count'])) {
                    $merchant_now_data_sum[$child_company_values_key]['count'] = 0;
                }
                if (!isset($merchant_now_data_sum[$child_company_values_key]['cash'])) {
                    $merchant_now_data_sum[$child_company_values_key]['cash'] = 0;
                }
                if (!isset($merchant_now_data_sum[$child_company_values_key]['deduct_cash'])) {
                    $merchant_now_data_sum[$child_company_values_key]['deduct_cash'] = 0;
                }
                if (!isset($merchant_now_data_sum[$child_company_values_key]['profit'])) {
                    $merchant_now_data_sum[$child_company_values_key]['profit'] = 0;
                }

                $merchant_now_data_sum[$child_company_values_key]['count'] += $child_company_values[$child_company_values_key]['count'];
                $merchant_now_data_sum[$child_company_values_key]['cash'] += $child_company_values[$child_company_values_key]['cash'];
                $merchant_now_data_sum[$child_company_values_key]['deduct_cash'] += $child_company_values[$child_company_values_key]['deduct_cash'];
                $merchant_now_data_sum[$child_company_values_key]['profit'] += $child_company_values[$child_company_values_key]['profit'];

                $all_sum['count'] += $child_company_values[$child_company_values_key]['count'];
                $all_sum['cash'] += $child_company_values[$child_company_values_key]['cash'];
                $all_sum['deduct_cash'] += $child_company_values[$child_company_values_key]['deduct_cash'];
                $all_sum['profit'] += $child_company_values[$child_company_values_key]['profit'];
            }
        }

        // 所有的代付公司子商户号
        $all_child_company = PaymentPaidCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $merchant_sum_count = count($merchant_now_data);

        // 所有的订单状态
        $all_status = [
            1 => '处理中',
            2 => '成功',
            3 => '失败',
            4 => '异常',
        ];

        $all_child_company_array = array_merge($all_child_company_array_bank, $all_child_company_array_alipay);

        // 排序
        $all_child_company_arrays = [];
        foreach ($all_child_company as $all_child_company_k => $all_child_company_v) {
            if (in_array($all_child_company_k, $all_child_company_array)) {
                $all_child_company_arrays[] = $all_child_company_k;
            }
        }

        $all_merchant_id_array = array_merge($all_merchant_id_array_bank, $all_merchant_id_array_alipay);

        $this->assign('merchant_sum_count', $merchant_sum_count);
        $this->assign('all_merchant_id_array', $all_merchant_id_array);
        $this->assign('all_child_company_array', $all_child_company_arrays);
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('child_company', $child_company);
        $this->assign('paid_class', $paid_class);
        $this->assign('all_status', $all_status);
        $this->assign('status', $status);
        $this->assign('paid_mode', $paid_mode);
        $this->assign('merchant_now_data', $merchant_now_data);
        $this->assign('merchant_now_data_sum', $merchant_now_data_sum);
        $this->assign('all_sum', $all_sum);
        return $this->fetch();
    }

    /**
     * 代付时间分析
     * @return mixed
     */
    public function paid_order_time()
    {
        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d', strtotime('-6 days'))));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d')));

        $merchant_id = input('merchant_id');
        $auth = true;
        if ($this->no_auth) {
            $merchant_id = $this->merchant_id;
            $auth = false;
        }
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $paid_class = input('paid_class');

        $map[] = ['status', '=', 2];

        $sum_day = ceil((strtotime($query_end_time) - strtotime($query_begin_time)) / 86400) + 1;

        $now_data = [];
        $now_order_count_ok = 0;
        $now_order_cash_ok = 0;
        $now_order_deduct_cash_ok = 0;
        $now_order_profit_ok = 0;

        // 银行卡代付
        if ($paid_class == 1) {
            // 交易统计
            for ($index = 0; $index < $sum_day; $index++) {
                $now_day = date('Y-m-d', (strtotime($query_end_time) - 3600 * 24 * $index));
                $map[] = ['create_time', 'between time', ["$now_day 00:00:00", "$now_day 23:59:59"]];
                $now_data[$now_day]['now_order_count_ok'] = PaidOrderModel::where($map)->count();
                $now_data[$now_day]['now_order_cash_ok'] = PaidOrderModel::where($map)->sum('cash');
                $now_data[$now_day]['now_order_cash_ok'] /= 100;
                $now_data[$now_day]['now_order_deduct_cash_ok'] = PaidOrderModel::where($map)->sum('deduct_cash');
                $now_data[$now_day]['now_order_deduct_cash_ok'] /= 100;
                $now_data[$now_day]['now_order_profit_ok'] = $now_data[$now_day]['now_order_deduct_cash_ok'] - $now_data[$now_day]['now_order_cash_ok'];
                static $now_order_count_ok = 0;
                static $now_order_cash_ok = 0;
                static $now_order_deduct_cash_ok = 0;
                static $now_order_profit_ok = 0;
                $now_order_count_ok = $now_order_count_ok + $now_data[$now_day]['now_order_count_ok'];
                $now_order_cash_ok = $now_order_cash_ok + $now_data[$now_day]['now_order_cash_ok'];
                $now_order_deduct_cash_ok = $now_order_deduct_cash_ok + $now_data[$now_day]['now_order_deduct_cash_ok'];
                $now_order_profit_ok = $now_order_profit_ok + $now_data[$now_day]['now_order_profit_ok'];
                // 去掉数组时间
                array_pop($map);
            }
        }

        // 支付宝代付
        if ($paid_class == 2) {
            // 交易统计
            for ($index = 0; $index < $sum_day; $index++) {
                $now_day = date('Y-m-d', (strtotime($query_end_time) - 3600 * 24 * $index));
                $map[] = ['create_time', 'between time', ["$now_day 00:00:00", "$now_day 23:59:59"]];
                $now_data[$now_day]['now_order_count_ok'] = AlipayPaidOrderModel::where($map)->count();
                $now_data[$now_day]['now_order_cash_ok'] = AlipayPaidOrderModel::where($map)->sum('cash');
                $now_data[$now_day]['now_order_cash_ok'] /= 100;
                $now_data[$now_day]['now_order_deduct_cash_ok'] = AlipayPaidOrderModel::where($map)->sum('deduct_cash');
                $now_data[$now_day]['now_order_deduct_cash_ok'] /= 100;
                $now_data[$now_day]['now_order_profit_ok'] = $now_data[$now_day]['now_order_deduct_cash_ok'] - $now_data[$now_day]['now_order_cash_ok'];
                static $now_order_count_ok = 0;
                static $now_order_cash_ok = 0;
                static $now_order_deduct_cash_ok = 0;
                static $now_order_profit_ok = 0;
                $now_order_count_ok = $now_order_count_ok + $now_data[$now_day]['now_order_count_ok'];
                $now_order_cash_ok = $now_order_cash_ok + $now_data[$now_day]['now_order_cash_ok'];
                $now_order_deduct_cash_ok = $now_order_deduct_cash_ok + $now_data[$now_day]['now_order_deduct_cash_ok'];
                $now_order_profit_ok = $now_order_profit_ok + $now_data[$now_day]['now_order_profit_ok'];
                // 去掉数组时间
                array_pop($map);
            }
        }

        if ($paid_class == '') {
            // 交易统计
            for ($index = 0; $index < $sum_day; $index++) {
                $now_day = date('Y-m-d', (strtotime($query_end_time) - 3600 * 24 * $index));
                $map[] = ['create_time', 'between time', ["$now_day 00:00:00", "$now_day 23:59:59"]];
                $now_data[$now_day]['now_order_count_ok_yhk'] = PaidOrderModel::where($map)->count();
                $now_data[$now_day]['now_order_count_ok_zfb'] = AlipayPaidOrderModel::where($map)->count();
                $now_data[$now_day]['now_order_count_ok'] = $now_data[$now_day]['now_order_count_ok_yhk'] + $now_data[$now_day]['now_order_count_ok_zfb'];
                $now_data[$now_day]['now_order_cash_ok_yhk'] = PaidOrderModel::where($map)->sum('cash');
                $now_data[$now_day]['now_order_cash_ok_zfb'] = AlipayPaidOrderModel::where($map)->sum('cash');
                $now_data[$now_day]['now_order_cash_ok'] = $now_data[$now_day]['now_order_cash_ok_yhk'] + $now_data[$now_day]['now_order_cash_ok_zfb'];
                $now_data[$now_day]['now_order_cash_ok'] /= 100;
                $now_data[$now_day]['now_order_deduct_cash_ok_yhk'] = PaidOrderModel::where($map)->sum('deduct_cash');
                $now_data[$now_day]['now_order_deduct_cash_ok_zfb'] = AlipayPaidOrderModel::where($map)->sum('deduct_cash');
                $now_data[$now_day]['now_order_deduct_cash_ok'] = $now_data[$now_day]['now_order_deduct_cash_ok_yhk'] + $now_data[$now_day]['now_order_deduct_cash_ok_zfb'];
                $now_data[$now_day]['now_order_deduct_cash_ok'] /= 100;
                $now_data[$now_day]['now_order_profit_ok'] = $now_data[$now_day]['now_order_deduct_cash_ok'] - $now_data[$now_day]['now_order_cash_ok'];
                static $now_order_count_ok = 0;
                static $now_order_cash_ok = 0;
                static $now_order_deduct_cash_ok = 0;
                static $now_order_profit_ok = 0;
                $now_order_count_ok = $now_order_count_ok + $now_data[$now_day]['now_order_count_ok'];
                $now_order_cash_ok = $now_order_cash_ok + $now_data[$now_day]['now_order_cash_ok'];
                $now_order_deduct_cash_ok = $now_order_deduct_cash_ok + $now_data[$now_day]['now_order_deduct_cash_ok'];
                $now_order_profit_ok = $now_order_profit_ok + $now_data[$now_day]['now_order_profit_ok'];
                // 去掉数组时间
                array_pop($map);
            }
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPaidCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $sum_count = $sum_day;

        $this->assign('sum_count', $sum_count);
        $this->assign('now_order_count_ok', $now_order_count_ok);
        $this->assign('now_order_cash_ok', $now_order_cash_ok);
        $this->assign('now_order_deduct_cash_ok', $now_order_deduct_cash_ok);
        $this->assign('now_order_profit_ok', $now_order_profit_ok);
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('child_company', $child_company);
        $this->assign('paid_class', $paid_class);
        $this->assign('now_data', $now_data);
        $this->assign('auth', $auth);
        return $this->fetch();
    }

    /**
     * 订单时间分析
     * @return mixed
     */
    public function order_time()
    {
        $query_begin_time = urldecode(input('query_begin_time', date('Y-m-d', strtotime('-6 days'))));
        $query_end_time = urldecode(input('query_end_time', date('Y-m-d')));

        $merchant_id = input('merchant_id');
        $auth = true;
        if ($this->no_auth) {
            $merchant_id = $this->merchant_id;
            $auth = false;
        }
        if ($merchant_id) {
            $merchant_config = MconfigModel::where(['id' => $merchant_id])->find();
            if (!$merchant_config) {
                $this->error('商户号不存在!');
            }
            $map[] = ['merchant_id', '=', $merchant_id];
        }

        $child_company = input('child_company');
        if ($child_company) {
            $map[] = ['child_company', '=', $child_company];
        }

        $pay_type = input('pay_type');
        if ($pay_type) {
            $map[] = ['type', '=', $pay_type];
        }

        $sum_day = ceil((strtotime($query_end_time) - strtotime($query_begin_time)) / 86400) + 1;

        $now_data = [];
        $now_order_count_sum = 0;
        $now_order_count_ok = 0;
        $now_order_cash_sum = 0;
        $now_order_cash_ok = 0;
        // 交易统计
        for ($index = 0; $index < $sum_day; $index++) {
            $now_day = date('Y-m-d', (strtotime($query_end_time) - 3600 * 24 * $index));
            $map[] = ['create_time', 'between time', ["$now_day 00:00:00", "$now_day 23:59:59"]];
            $now_data[$now_day]['now_order_count_sum'] = OrderModel::where($map)->count();
            $now_data[$now_day]['now_order_count_ok'] = OrderModel::where($map)->where(['status' => 2])->count();
            $now_data[$now_day]['now_order_cash_sum'] = OrderModel::where($map)->sum('cash');
            $now_data[$now_day]['now_order_cash_sum'] /= 100;
            $now_data[$now_day]['now_order_cash_ok'] = OrderModel::where($map)->where(['status' => 2])->sum('cash');
            $now_data[$now_day]['now_order_cash_ok'] /= 100;
            static $now_order_count_sum = 0;
            static $now_order_count_ok = 0;
            static $now_order_cash_sum = 0;
            static $now_order_cash_ok = 0;
            $now_order_count_sum = $now_order_count_sum + $now_data[$now_day]['now_order_count_sum'];
            $now_order_count_ok = $now_order_count_ok + $now_data[$now_day]['now_order_count_ok'];
            $now_order_cash_sum = $now_order_cash_sum + $now_data[$now_day]['now_order_cash_sum'];
            $now_order_cash_ok = $now_order_cash_ok + $now_data[$now_day]['now_order_cash_ok'];
            // 去掉数组时间
            array_pop($map);
        }

        // 所有的支付公司子商户号
        $all_child_company = PaymentPayCompanyModel::where([])->order(['group' => 'asc', 'sort' => 'asc', 'title' => 'asc', 'name' => 'asc'])->column('title', 'name');

        $sum_count = $sum_day;

        $this->assign('sum_count', $sum_count);
        $this->assign('now_order_count_sum', $now_order_count_sum);
        $this->assign('now_order_count_ok', $now_order_count_ok);
        $this->assign('now_order_cash_sum', $now_order_cash_sum);
        $this->assign('now_order_cash_ok', $now_order_cash_ok);
        $this->assign('all_type', pay_config('payment_type'));
        $this->assign('all_child_company', $all_child_company);
        $this->assign('query_begin_time', $query_begin_time);
        $this->assign('query_end_time', $query_end_time);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('child_company', $child_company);
        $this->assign('pay_type', $pay_type);
        $this->assign('now_data', $now_data);
        $this->assign('auth', $auth);
        return $this->fetch();
    }
}
